package com.treasure.mall.biz.entity.converters;

import cn.hutool.core.util.ArrayUtil;
import org.springframework.util.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/8/22
 */
@Converter(autoApply = true)
public class StringArrayConverter implements AttributeConverter<String[], String> {
    @Override
    public String convertToDatabaseColumn(String[] attribute) {
        return ArrayUtil.join(attribute, ",");
    }

    @Override
    public String[] convertToEntityAttribute(String dbData) {
        if (StringUtils.isEmpty(dbData)) {
            return new String[0];
        }
        return dbData.split(",");
    }
}
